package org.wuxinshui.boosters.interview01.lock;

import java.util.concurrent.ConcurrentHashMap;

public class LockStatusExample {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        // 偏向锁延迟默认4秒，可通过 -XX:BiasedLockingStartupDelay=0 立即生效
        Thread.sleep(5000);

        synchronized (lock) { // 第一次加锁，触发偏向锁
            System.out.println("偏向锁状态：Mark Word 包含线程ID");
        }

        new Thread(() -> {
            synchronized (lock) { // 第二个线程竞争，升级为轻量级锁
                System.out.println("轻量级锁状态：CAS自旋竞争");
            }
        }).start();

        Thread.sleep(1000);

        for (int i = 0; i < 3; i++) { // 高竞争，升级为重量级锁
            new Thread(() -> {
                synchronized (lock) {
                    System.out.println("重量级锁状态：线程阻塞等待");
                }
            }).start();
        }
    }


    static final class ForwardingNode{


        Integer find(int h, Object k) {
            // loop to avoid arbitrarily deep recursion on forwarding nodes
            outer: for (;;) {
                return 1;
            }
        }
    }
}

